package com.zbiti.anvil.basic.admin.gateway.selenium.util;

import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * UI页面触发器
 *
 * @author Fanyc
 * @date 2021-05-14 14:34
 */
public class UiTriggerHelper {

    /**
     * 点击按钮
     *
     * @param webElement 页面元素
     * @param buttonName 按钮名称
     * @throws InterruptedException Sleep异常
     */
    public static void clickButton(WebElement webElement, String buttonName) throws InterruptedException {
        List<WebElement> buttons = ElementUtils.cssSelectorAll(webElement, "button");
        Optional<WebElement> button = buttons.stream().filter(b -> buttonName.equals(b.getText())).findFirst();
        assertTrue(button.isPresent(), String.format("按钮【%s】未找到", buttonName));
        ElementUtils.click(button.get());
    }

    /**
     * 下拉框选择
     *
     * @param elSelect 下拉框
     * @param index    选择第几个，从0开始
     * @throws InterruptedException Sleep异常
     */
    public static void elSelectChange(WebElement elSelect, int index) throws InterruptedException {
        // 触发显示下拉框
        ElementUtils.clickByCssSelector(elSelect, "span[class='el-input__suffix']");
        // 找到显示的下拉框
        WebElement elSelectDropdown = ElementUtils.cssSelector(null, "div[class~='el-select-dropdown'][x-placement='bottom-start']");
        // 获取下拉选项
        List<WebElement> options = ElementUtils.cssSelectorAll(elSelectDropdown, "li[class~='el-select-dropdown__item']");
        // 执行点击
        ElementUtils.click(options.get(index));
    }

    /**
     * 点击查询按钮
     *
     * @param searchForm 查询区域
     * @param mask       遮罩层
     * @throws InterruptedException Sleep异常
     */
    public static void clickSearchButton(WebElement searchForm, WebElement mask) throws InterruptedException {
        clickButton(searchForm, "搜索");
        ElementUtils.waitInvisibility(mask);
    }

    /**
     * 点击重置按钮
     *
     * @param searchForm 查询区域
     * @param mask       遮罩层
     * @throws InterruptedException Sleep异常
     */
    public static void clickResetButton(WebElement searchForm, WebElement mask) throws InterruptedException {
        clickButton(searchForm, "重置");
        ElementUtils.waitInvisibility(mask);
    }

    /**
     * 点击工具栏的按钮
     *
     * @param toolbar    工具栏对象
     * @param buttonName 按钮名称
     * @param waitModal  是否为打开的Dialog
     * @throws InterruptedException Sleep异常
     */
    public static void clickToolbarButton(WebElement toolbar, String buttonName, boolean waitModal) throws InterruptedException {
        clickButton(toolbar, buttonName);
        if (waitModal) {
            ElementUtils.waitDialogVisibility();
        }
    }

    /**
     * 点击对话框按钮
     *
     * @param elDialog 对话框元素
     * @throws InterruptedException Sleep异常
     */
    public static void clickElDialogButton(WebElement elDialog, String buttonName) throws InterruptedException {
        WebElement footer = ElementUtils.elDialogFooter(elDialog);
        clickButton(footer, buttonName);
        ElementUtils.waitDialogInvisibility();
    }

}
